/*
 * @Author: huangjinyao huangjinyao@hjy.cn
 * @Date: 2024-07-03 20:50:21
 * @LastEditors: huangjinyao huangjinyao@hjy.cn
 * @LastEditTime: 2024-07-05 17:20:28
 * @FilePath: \小兔仙项目\vue-rabbit\src\stores\user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginAPI } from '@/apis/user'
import { userCarStore } from "./cartStore";
import { mergeCartAPI } from '@/apis/cart';


//import * as clearCart from './cartStore'
export const useUserStore = defineStore('user', () => {
    const cartStore = userCarStore();
    const userInfo = ref({})
    // 登录
    const getUserInfo = async ({ account, password }) => {
        const res = await loginAPI({ account, password })
        userInfo.value = res.result
        await mergeCartAPI(cartStore.cartList.map(item => {
            return {
                skuId: item.skuId,
                selected: item.selected,
                count: item.count
            }
        }))

        cartStore.updateNewList()
    }


    const clearUserInfo = () => {
        userInfo.value = {}
        cartStore.clearCart()
        //clearCart()
    }
    return { userInfo, getUserInfo, clearUserInfo }

}, {
    persist: true
}
)